package com.foolishbird.sdk.ui.content;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.core.app.JobIntentService;

import com.foolishbird.sdk.assist.Constant;
import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.core.HttpEngineConduits;
import com.foolishbird.sdk.params.bean.PopupConfig;
import com.foolishbird.sdk.utils.WL;

import java.util.List;

public class TimeTaskService extends JobIntentService implements Handler.Callback {

    private static final String TAG = TimeTaskService.class.getSimpleName();

    private static final int JOB_ID = 10000;
    private static final long TIMED_TASK_SLEEP_MILLIS_UNIT = 1000L;
    private static final long TIMED_TASK_SLEEP_MILLIS = 60 * TIMED_TASK_SLEEP_MILLIS_UNIT;

    private Handler mTimedTaskHandler;


    public static void enqueueWork(Context context, int seconds) {
//        if (popupConfigs != null) {
//            for (PopupConfig config : popupConfigs) {
//                if (config != null) {
//                    Intent work = new Intent();
//                    work.putExtra(Constant.LocalIntentExtra.INTENT_KEY_LOGIN_BINDING_DATA, config);
//                    enqueueWork(context, TimeTaskService.class, JOB_ID, work);
//                }
//            }
//        }
        Intent work = new Intent();
        work.putExtra(Constant.LocalIntentExtra.INTENT_KEY_LOGIN_BINDING_DATA, seconds);
        enqueueWork(context, TimeTaskService.class, JOB_ID, work);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        WL.d(TAG, "onCreate ===========>");
        mTimedTaskHandler = new Handler(this);
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        WL.d(TAG, "onHandleWork ===========>");

//        if (!isStopService()) {
            int seconds = intent.getIntExtra(Constant.LocalIntentExtra.INTENT_KEY_LOGIN_BINDING_DATA,180);
            serviceThreadSleep(TIMED_TASK_SLEEP_MILLIS_UNIT*seconds);

            mTimedTaskHandler.sendMessage(mTimedTaskHandler.obtainMessage(0, null));
//            if (popupConfig != null) {
//                serviceThreadSleep("s".equals(popupConfig.getIntervalUnit())?
//                        popupConfig.getIntervalTimes() * TIMED_TASK_SLEEP_MILLIS_UNIT
//                        :popupConfig.getIntervalTimes() * TIMED_TASK_SLEEP_MILLIS);
//
//                if (!isStopService()) {
//                    mTimedTaskHandler.sendMessage(mTimedTaskHandler.obtainMessage(0, popupConfig));
//                }
//            }
//        }

        WL.d(TAG, "onHanleWork ===========>sleep finish.");
    }

//    private boolean isStopService() {
////        //如果已经进行过身份证绑定实名认证，就停止服务
////        HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
////        if (conduits != null && conduits.isLoggedIn()) {
////            conduits.writeNeedIdentify(true);
//////            RealNameInfo realNameInfo = conduits.getRealnameInfo();
//////            if (realNameInfo != null) {
//////                return realNameInfo.isRealNameCertified();
//////            }
////        }
//
//        return false;
//    }

    private void serviceThreadSleep(long millis) {
        WL.d(TAG, "serviceThreadsleep ===========>sleep millis: " + millis);
        final long enterMethodTimeMillis = System.currentTimeMillis();
        try {
            long currentTimeMillis = System.currentTimeMillis();
            while (Math.abs(currentTimeMillis - enterMethodTimeMillis) < millis ) {
                Thread.sleep(TIMED_TASK_SLEEP_MILLIS_UNIT);
                currentTimeMillis = System.currentTimeMillis();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            WL.d(TAG, "serviceThreadsleep ===========>InterruptedException.");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        WL.d(TAG, "onDestroy ===========>");
    }

    @Override
    public boolean handleMessage(Message msg) {
        WL.d(TAG, "handleMessage ===========>");
//        PopupConfig popupConfig = (PopupConfig) msg.obj;
//        if(popupConfig.isIdcardPopup() || popupConfig.isPhonePopup()){
//            Intent broadcast = new Intent(Constant.LocalIntentAction.INTENT_AACTION_FORCE_REALNAME_CERTIFIES);
//            broadcast.putExtra("isIdcardPopupCloseEnable", popupConfig.isIdcardPopupCloseEnable());
////            LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(broadcast);
//            sendBroadcast(broadcast);
            if(!HttpEngineApi.getInstance().getEngineConduits().readNeedBindPhone())return true;
            Intent newIntent = new Intent(Constant.LocalIntentAction.INTENT_AACTION_START_LOGIN_ACTIVITY);
            newIntent.setPackage(getApplicationContext().getPackageName());
            newIntent.putExtra(Constant.LocalIntentExtra.INTENT_KEY_LOGIN_INIT_POSITION, Constant.LocalIntentExtra.INTENT_VALUE_LOGIN_INIT_BINDING);
//            newIntent.putExtra(Constant.LocalIntentExtra.INTENT_KEY_LOGIN_BINDING_DATA, popupConfig);
            newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getApplicationContext().startActivity(newIntent);
//        }
        return true;
    }

}
